/*
package com.jhws.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.jhws.common.PropertiesReader;
import com.jhws.business.visitor.bo.TblVisitorDimForm;
import org.apache.commons.io.FilenameUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

*/
/**
 * This sample demonstrates how to list objects under specfied bucket from
 * Aliyun OSS using the OSS SDK for Java.
 *//*

public class ListObjectsOss {

    private String endpoint;
    private String accessKeyId = "AqiuCaa7GiB5QWiz";
    private String accessKeySecret = "EJFuBWBYgqh1FxqvoBAEAjMky5TAGj";
    private String bucketNameVisitors = "visitors";
    private String bucketNameGerneralfiles = "gerneralfiles";
    private String baseUrlGerneralfiles = "http://gerneralfiles.oss-cn-shenzhen.aliyuncs.com/";
    private OSSClient client;
    private Date expiration;
    private URL url;
    TblVisitorDimForm tblVisitorDimForm;

    public ListObjectsOss() {
        endpoint = PropertiesReader.getProperty("ossUrl");
        String basePlatform = PropertiesReader.getProperty("basePlatform");
        if (!StringUtils.isEmpty(basePlatform)) {
            bucketNameVisitors = basePlatform + "-" + bucketNameVisitors;
            bucketNameGerneralfiles = basePlatform + "-" + bucketNameGerneralfiles;
            baseUrlGerneralfiles = "http://" + basePlatform + "-" + "gerneralfiles.oss-cn-shenzhen.aliyuncs.com/";
        }
        client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }

    public String getBucketNameVisitors() {
        return bucketNameVisitors;
    }

    public String getBucketNameGerneralfiles() {
        return bucketNameGerneralfiles;
    }

    public String getBaseUrlGerneralfiles() {
        return baseUrlGerneralfiles;
    }

    private List<OSSObjectSummary> getOssObjectSummarys(String Prefix) {

		*/
/*
         * List objects under the bucket
		 *//*

        ObjectListing objectListing = null;
        final int maxKeys = 500;

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketNameVisitors);
        listObjectsRequest.setPrefix(Prefix);
        listObjectsRequest.withMaxKeys(maxKeys);
        objectListing = client.listObjects(listObjectsRequest);

        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        return sums;
    }

    public List<TblVisitorDimForm> getVisitor(String nextPrefix) {
        List<TblVisitorDimForm> tblVisitorDimForms = new ArrayList<TblVisitorDimForm>();
        try {
            List<OSSObjectSummary> sums = getOssObjectSummarys(nextPrefix);

            for (OSSObjectSummary s : sums) {
                // 设置URL过期时间为1小时
                expiration = new Date(new Date().getTime() + 3600 * 1000);
                // 生成URL
                url = client.generatePresignedUrl(bucketNameVisitors, s.getKey(), expiration);
                tblVisitorDimForms.add(getVisitorDim(s.getKey(), url.toString()));
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
        }
        return tblVisitorDimForms;
    }

    public String getVisitorUrl(String path) {
        // 设置URL过期时间为1小时
        expiration = new Date(new Date().getTime() + 3600 * 1000);
        // 生成URL
        URL url = client.generatePresignedUrl(bucketNameVisitors, path, expiration);
        return url.toString();
    }

    public InputStream getVisitorContent(String path) {
        return client.getObject(bucketNameGerneralfiles, path).getObjectContent();
    }

    public List<String> getOssObjectList(String Prefix) {
        List<OSSObjectSummary> sums = getOssObjectSummarys(Prefix);
        List<String> urlList = new ArrayList<String>();
        for (OSSObjectSummary s : sums) {
            urlList.add(s.getKey());
        }
        return urlList;
    }

    public TblVisitorDimForm getVisitorDim(String string, String url) {
        String[] strings = string.split("/");
        int len = strings.length;
        tblVisitorDimForm = new TblVisitorDimForm();
        tblVisitorDimForm.setEquCode(strings[len - 3]);
        //这里单独处理，别墅机没有RoomCode
        try {
            tblVisitorDimForm.setRoomCode(AccountsUtils.getBuildingInfo(strings[len - 2]));
        } catch (Exception e) {
            // TODO: handle exception
        }
        tblVisitorDimForm.setVisiTime(strings[len - 4]);
        tblVisitorDimForm.setVisiImg(strings[len - 1]);
        tblVisitorDimForm.setImgUrl(url);
        return tblVisitorDimForm;
    }

    //isOss,是否去oss获取url
    public String getUrl(String nextPrefix, boolean isOss) {
        if (isOss) {
            ObjectListing objectListing = null;
            final int maxKeys = 500;
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketNameGerneralfiles);
            listObjectsRequest.setPrefix(nextPrefix);
            listObjectsRequest.withMaxKeys(maxKeys);
            objectListing = client.listObjects(listObjectsRequest);

            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                String key = s.getKey();
                if (key.charAt(key.length() - 1) == '/')
                    continue;
                // 生成URL
                return baseUrlGerneralfiles + key;
            }
        }
        return baseUrlGerneralfiles + nextPrefix;
    }

    public void updateFile(String key, byte[] bytes) {
        // String key = "MyObjectKey";
        client.putObject(bucketNameGerneralfiles, key, new ByteArrayInputStream(bytes));
    }

    public void updateFile(String key, InputStream inputStream) {
        // String key = "MyObjectKey";
        client.putObject(bucketNameGerneralfiles, key, inputStream);
    }

    public void localUpdateFile(String key, String localFilePath) {
        // String key = "MyObjectKey";
        PutObjectResult r = client.putObject(bucketNameGerneralfiles, key, new File(localFilePath));
    }

    */
/**
     * gerneral拷贝
     *
     * @param key
     * @param destKey
     * @return
     *//*

    public CopyObjectResult gerneralCopyFile(String key, String destKey) {
        String folder = FilenameUtils.getFullPath(destKey);
        // Folder是否存在
        boolean found = client.doesObjectExist(bucketNameGerneralfiles, folder);
        if (!found) {
            client.putObject(bucketNameGerneralfiles, folder, new ByteArrayInputStream(new byte[0]));
        }
        // 拷贝Object
        return client.copyObject(bucketNameGerneralfiles, key, bucketNameGerneralfiles, destKey);
    }

    */
/**
     * visitors 拷贝
     *
     * @param key
     * @param destKey
     * @return
     *//*

    public CopyObjectResult visitorsCopyFile(String key, String destKey) {
        CopyObjectResult copyObjectResult = null;
        try {
            // 拷贝Object
            copyObjectResult = client.copyObject(bucketNameVisitors, key, bucketNameVisitors, destKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return copyObjectResult;
    }

    public void deleteObject(String key) {
        // 删除Object
        client.deleteObject(bucketNameGerneralfiles, key);
    }

    public void destroy() {
        if (client != null) {
            client.shutdown();
            client = null;
        }
    }

    // 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
    private final String REGION_CN_HANGZHOU = "cn-hangzhou";
    // 当前 STS API 版本
    private final String STS_API_VERSION = "2015-04-01";

    public AssumeRoleResponse.Credentials getCredentials() throws Exception {
        // 只有 RAM用户（子账号）才能调用 AssumeRole 接口
        // 阿里云主账号的AccessKeys不能用于发起AssumeRole请求
        // 请首先在RAM控制台创建一个RAM用户，并为这个用户创建AccessKeys
        String accessKeyId = "LTAIUkJukV6vkcwN";
        String accessKeySecret = "fGTrOGc5ZC4mzXhXBrnoJtfRqIr5UC";
        // AssumeRole API 请求参数: RoleArn, RoleSessionName, Policy, and DurationSeconds
        // RoleArn 需要在 RAM 控制台上获取
        String roleArn = "acs:ram::1264344271270439:role/oss";
        // RoleSessionName 是临时Token的会话名称，自己指定用于标识你的用户，主要用于审计，或者用于区分Token颁发给谁
        // 但是注意RoleSessionName的长度和规则，不要有空格，只能有'-' '_' 字母和数字等字符
        // 具体规则请参考API文档中的格式要求
        String roleSessionName = "alice-001";
        // 如何定制你的policy?
        String policy = "{\n" +
                "  \"Statement\": [\n" +
                "    {\n" +
                "      \"Action\": \"oss:*\",\n" +
                "      \"Effect\": \"Allow\",\n" +
                "      \"Resource\": \"*\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"Version\": \"1\"\n" +
                "}";
        // 此处必须为 HTTPS
        ProtocolType protocolType = ProtocolType.HTTPS;
        final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
                roleArn, roleSessionName, policy, protocolType);
        return response.getCredentials();
    }

    private AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
                                          String roleArn, String roleSessionName, String policy,
                                          ProtocolType protocolType) throws Exception {
        // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
        IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        // 创建一个 AssumeRoleRequest 并设置请求参数
        final AssumeRoleRequest request = new AssumeRoleRequest();
        request.setVersion(STS_API_VERSION);
        request.setMethod(MethodType.POST);
        request.setProtocol(protocolType);
        request.setRoleArn(roleArn);
        request.setRoleSessionName(roleSessionName);
        request.setPolicy(policy);
        // 发起请求，并得到response
        return client.getAcsResponse(request);
    }

    */
/*public static void main(String[] args) {
        // 只有 RAM用户（子账号）才能调用 AssumeRole 接口
        // 阿里云主账号的AccessKeys不能用于发起AssumeRole请求
        // 请首先在RAM控制台创建一个RAM用户，并为这个用户创建AccessKeys
        String accessKeyId = "LTAIUkJukV6vkcwN";
        String accessKeySecret = "fGTrOGc5ZC4mzXhXBrnoJtfRqIr5UC";
        // AssumeRole API 请求参数: RoleArn, RoleSessionName, Policy, and DurationSeconds
        // RoleArn 需要在 RAM 控制台上获取
        String roleArn = "acs:ram::1264344271270439:role/oss";
        // RoleSessionName 是临时Token的会话名称，自己指定用于标识你的用户，主要用于审计，或者用于区分Token颁发给谁
        // 但是注意RoleSessionName的长度和规则，不要有空格，只能有'-' '_' 字母和数字等字符
        // 具体规则请参考API文档中的格式要求
        String roleSessionName = "alice-001";
        // 如何定制你的policy?
        String policy = "{\n" +
                "  \"Statement\": [\n" +
                "    {\n" +
                "      \"Action\": \"oss:*\",\n" +
                "      \"Effect\": \"Allow\",\n" +
                "      \"Resource\": \"*\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"Version\": \"1\"\n" +
                "}";
        // 此处必须为 HTTPS
        ProtocolType protocolType = ProtocolType.HTTPS;
        try {
            final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
                    roleArn, roleSessionName, policy, protocolType);
            System.out.println("Expiration: " + response.getCredentials().getExpiration());
            System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
            System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
            System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
        } catch (ClientException e) {
            System.out.println("Failed to get a token.");
            System.out.println("Error code: " + e.getErrorCode());
            System.out.println("Error message: " + e.getErrorMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*//*

}
*/
